﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

// TODO: Complete documentation
namespace ComicTracker.Models
{
    [Table("series")]
    public class Series : IComparable<Series>
    {
        private Guid _id;
        private Guid _publisher_id;
        private Publisher _publisher;
        private string _name;
        private string _sort_name;
        private string _publish_frequency;
        private bool _complete;
        private List<Issue> _issues;

        string[] publish_freqs = new string[]{ "Annually", "Bi-Monthly", "Bi-Weekly", "Daily", "Irregular", 
                                                     "Monthly", "One-Shot", "Quarterly", "Weekly", "Yearly" };

        #region Properties

        [Key]
        [Column("id")]
        public Guid ID
        {
            get { return _id; }
            set { _id = value; }
        }

        [Column("publisher_id")]
        [Required(ErrorMessage="A publisher id is required.")]
        public Guid PublisherID
        {
            get { return _publisher_id; }
            set { _publisher_id = value; }
        }

        public virtual Publisher Publisher
        {
            get { return _publisher; }
            set { _publisher = value; }
        }

        [Column("name")]
        [Required(ErrorMessage="A name is required.")]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        [Column("sort_name")]
        [Display(Name="Sort Name")]
        public string SortName
        {
            get { return _sort_name; }
            set { _sort_name = value; }
        }

        [Column("publish_frequency")]
        [Display(Name="Publication Frequency")]
        public string PublishFrequency
        {
            get { return _publish_frequency; }
            set
            {
                if (publish_freqs.Contains(value))
                    _publish_frequency = value;
            }
        }

        [Column("complete")]
        [Required(ErrorMessage="Series must be marked as complete, or ongoing.")]
        public bool Complete
        {
            get { return _complete; }
            set { _complete = value; }
        }

        public virtual List<Issue> Issues
        {
            get 
            {
                if (_issues != null)
                    _issues.Sort();
                return _issues; 
            }
            set 
            { 
                _issues = value;
            }
        }
        #endregion

        public int CompareTo(Series other)
        {
            if (other == null)
                return 1;

            // Compare publishers
            int cmp = this.Publisher.CompareTo(other.Publisher);
            if (cmp == 0)
            {
                // Compare sort names if available
                if (!String.IsNullOrWhiteSpace(this.SortName))
                {
                    if (!String.IsNullOrWhiteSpace(other.SortName))
                        return this.SortName.CompareTo(other.SortName);
                    else
                        return this.SortName.CompareTo(other.Name);
                }

                if (!String.IsNullOrWhiteSpace(other.SortName))
                    return this.Name.CompareTo(other.SortName);
                else
                    return this.Name.CompareTo(other.Name);
            }

            return cmp;
        }
    }
}